<?php require_once('../Connections/locale.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE province SET id_regione=%s, nome_provincia=%s, sigla_provincia=%s WHERE id_provincia=%s",
                       GetSQLValueString($_POST['id_regione'], "int"),
                       GetSQLValueString($_POST['nome_provincia'], "text"),
                       GetSQLValueString($_POST['sigla_provincia'], "text"),
                       GetSQLValueString($_POST['id_provincia'], "int"));

  mysql_select_db($database_locale, $locale);
  $Result1 = mysql_query($updateSQL, $locale) or die(mysql_error());

  $updateGoTo = "elenco_province.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_locale, $locale);
$query_regioni = "SELECT * FROM regioni";
$regioni = mysql_query($query_regioni, $locale) or die(mysql_error());
$row_regioni = mysql_fetch_assoc($regioni);
$totalRows_regioni = mysql_num_rows($regioni);

$colname_province = "-1";
if (isset($_GET['id_provincia'])) {
  $colname_province = (get_magic_quotes_gpc()) ? $_GET['id_provincia'] : addslashes($_GET['id_provincia']);
}
mysql_select_db($database_locale, $locale);
$query_province = sprintf("SELECT * FROM province WHERE id_provincia = %s", $colname_province);
$province = mysql_query($query_province, $locale) or die(mysql_error());
$row_province = mysql_fetch_assoc($province);
$totalRows_province = mysql_num_rows($province);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
<link href="../style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="left">
    <tr valign="baseline">
      <td nowrap align="right">Id:</td>
      <td><?php echo $row_province['id_provincia']; ?></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Regione:</td>
      <td><select name="id_regione">
        <?php 
do {  
?>
        <option value="<?php echo $row_regioni['id_regione']?>" <?php if (!(strcmp($row_regioni['id_regione'], $row_province['id_regione']))) {echo "SELECTED";} ?>><?php echo $row_regioni['nome_regione']?></option>
        <?php
} while ($row_regioni = mysql_fetch_assoc($regioni));
?>
      </select>
      </td>
    <tr>
    <tr valign="baseline">
      <td nowrap align="right">Nome Provincia:</td>
      <td><input type="text" name="nome_provincia" value="<?php echo $row_province['nome_provincia']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Sigla Provincia:</td>
      <td><input type="text" name="sigla_provincia" value="<?php echo $row_province['sigla_provincia']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Update record"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1">
  <input type="hidden" name="id_provincia" value="<?php echo $row_province['id_provincia']; ?>">
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($regioni);

mysql_free_result($province);
?>
